package com.dolawing.exoplayerdemo2;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.google.android.exoplayer2.Player;

public class ExoPlayerService extends Service {
    private static final String TAG = "ExoPlayerService";

    private OnProgressLis onProgressLis;

    public OnProgressLis getOnProgressLis() {
        return onProgressLis;
    }

    public ExoPlayerService setOnProgressLis(OnProgressLis onProgressLis) {
        this.onProgressLis = onProgressLis;
        LogUtil.i("setOnProgressLis()");
        return this;
    }
    public class MusicBinder extends Binder {
        public ExoPlayerService getService() {
            return ExoPlayerService.this;
        }
    }

//    @Override
//    public int onStartCommand(Intent intent, int flags, int startId) {
//        LogUtil.i("执行了onStartCommand()");
//        return super.onStartCommand(intent, flags, startId);
//    }

    @Override
    public void onCreate() {
        LogUtil.i("onCreate");
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogUtil.i("music service onUnbind");
        ExoPlayerManager.getDefault().releasePlayer();
        return super.onUnbind(intent);
    }

    public void initListener() {
        ExoPlayerManager.getDefault().addMediaListener(new ExoPlayerManager.MediaControlListener() {
            @Override
            public void setCurPositionTime(long curPositionTime) {
                if (onProgressLis != null) {
                    onProgressLis.onPositionChanged(curPositionTime);
                }
            }

            @Override
            public void setDurationTime(long durationTime) {
                if (onProgressLis != null)
                    onProgressLis.onDurationChanged(durationTime);
            }

            @Override
            public void setBufferedPositionTime(long bufferedPosition) {
                if (onProgressLis != null) {
                    onProgressLis.onBufferedPositionChanged(bufferedPosition);
                }

            }

            @Override
            public void setCurTimeString(String curTimeString) {
                if (onProgressLis != null) {
                    onProgressLis.onCurTimeStringChanged(curTimeString);
                }
            }

            @Override
            public void setDurationTimeString(String durationTimeString) {
                if (onProgressLis != null) {
                    onProgressLis.onDurationTimeStringChanged(durationTimeString);
                }
            }
        });
        ExoPlayerManager.getDefault().addListener(new Player.EventListener() {
            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                Log.i(TAG,  "playWhenReady-->" + playWhenReady + "playbackState-->" + playbackState);
                if (onProgressLis != null) {
                    onProgressLis.onPlayerStateChanged(playWhenReady, playbackState);
                }
            }

            @Override
            public void onLoadingChanged(boolean isLoading) {
                if (isLoading) {
                    ExoPlayerManager.getDefault().startListenProgress();
                }
            }
        });
    }

    public interface OnProgressLis {
        void onPositionChanged(long curPositionTime);

        void onDurationChanged(long durationTime);

        void onBufferedPositionChanged(long bufferedPosition);

        void onCurTimeStringChanged(String curTimeString);

        void onDurationTimeStringChanged(String durationTimeString);

        void onPlayerStateChanged(boolean playWhenReady, int playbackState);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.i("music service destoryed");
        ExoPlayerManager.getDefault().releasePlayer();
    }

}
